#include "slist.h"

#define my_scanf(op, format, ...) do{           \
    int ret = -1;                               \
    ret = scanf(format, __VA_ARGS__);           \
    while('\n' != getchar())                    \
        /*do nothing*/;                         \
    if (ret <= 0)                               \
        op;                                     \
}while(0)


//自定义输出函数
int my_print(void *data)
{
    printf("%d ", *(int *)data);

    return 0;
}

//自定义比较函数
int my_cmp(void *data, void *key)
{
    return *(int *)data - *(int *)key;
}

int  op_write(void *data, FILE *fp)
{
    int ret = -1;

    if (NULL == data || NULL == fp)
    {
        DEBUG("some elements are NULL\n"); 
        goto err0;
    }

    ret =  fwrite(data, sizeof(int), 1, fp);
    if (ret <= 0)
    {
        DEBUG("fwite failed..\n"); 
        goto err0;
    }

    return 0;
err0:
    return -1;
}

int  op_read(void *data, FILE *fp)
{
    int ret = -1;

    if (NULL == data || NULL == fp)
    {
        DEBUG("some elements are NULL\n"); 
        goto err0;
    }

    ret =  fread(data, sizeof(int), 1, fp);
    if (ret <= 0)
    {
        DEBUG("fread failed..\n"); 
        goto err0;
    }

    return 0;
err0:
    return -1;
}

int main(void)
{
    int n;
    int key;
    int index;
    slist_t *s = NULL;
    slist_t *s1 = NULL;
    
    //头信息的创建
    s = slist_create(sizeof(int), NULL);
    if (NULL == s)
        goto err0;

    while(1)
    {
    retry:
        printf("please input a number[-1 quit]: ");
        my_scanf(goto retry, "%d", &n);

        //结束循环
        if (-1 == n)
            break;
    
        //尾插 
        slist_append(s, &n); 
        //头插
        //slist_prepend(s, &n);
    }

    //遍历
    slist_traverse(s, my_print);
    printf("\n");



#if 0
    printf("==============test slist_insert_by_index===============\n");
    printf("please input an index and value: ");
    my_scanf(, "%d %d", &index, &n);
    slist_insert_by_index(s, &n, index);
    slist_traverse(s, my_print);
    printf("\n");
#endif

#if 0
    printf("==========test slist_retrive_by_index=================\n");
    printf("please input an index: ");
    my_scanf(, "%d", &index);
    slist_retrieve_by_index(s, &n, index);
    printf("find index %d ---> %d\n", index, n);
#endif

#if 0
    printf("==========test slist_retrieve_by_key=================\n");
    printf("please input an key: ");
    my_scanf(, "%d", &key);
    slist_retrieve_by_key(s, &n, &key, my_cmp);
    printf("find key %d ---> %d\n", key, n);

#endif
    
#if 0
    printf("===========test slist_find_index_by_key=============\n");
    printf("please input a key: ");
    my_scanf(, "%d", &n);
    index = slist_find_index_by_key(s, &n, my_cmp);
    printf("find value: %d --> index : %d\n", n, index);
#endif

#if 0
    printf("==============test slist_find_all_index_by_key=========\n");
    printf("please input a key: ");
    my_scanf(, "%d", &n);
    s1 = slist_find_all_index_by_key(s, &n, my_cmp);
    slist_traverse(s1, my_print);
    printf("\n");
    slist_destroy(s1);
#endif

    
#if 0
    printf("=============test slist_modify_by_index====================\n");
    printf("please input index and value: ");
    my_scanf(, "%d %d", &index, &n);
    slist_modify_by_index(s, &n, index);
    slist_traverse(s, my_print);
    printf("\n");
#endif
    
#if 0
    printf("=============test slist_modify_by_key====================\n");
    printf("please input a key and value: ");
    my_scanf(, "%d %d", &key, &n);
    slist_modify_by_key(s, &n, &key, my_cmp);
    slist_traverse(s, my_print);
#endif

#if 0
    printf("=============test slist_modify_all_by_key====================\n");
    printf("please input a key and value: ");
    my_scanf(, "%d %d", &key, &n);
    slist_modify_all_by_key(s, &n, &key, my_cmp);
    slist_traverse(s, my_print);
#endif

#if 0
    printf("=============test slist_delete_by_index====================\n");
    printf("please input an index: ");
    my_scanf(, "%d", &index);
    slist_delete_by_index(s, index);
    slist_traverse(s, my_print);
#endif

#if 0
    printf("=============test slist_delete_by_key====================\n");
    printf("please input a key : ");
    my_scanf(, "%d", &n);
    slist_delete_by_key(s, &n, my_cmp);
    slist_traverse(s, my_print);
    printf("\n");
#endif

#if 0
    printf("=============test slist_delete_all_by_key====================\n");
    printf("please input a key : ");
    my_scanf(, "%d", &n);
    slist_delete_all_by_key(s, &n, my_cmp);
    slist_traverse(s, my_print);
#endif


    //slist_store(s, "txt", op_write);
#if 1
    s1 = slist_create(sizeof(int), NULL);
    if (NULL == s1)
    {
        DEBUG("slist create failed..\n"); 
        goto err0;
    }
    slist_load(s1, "txt", op_read);
    slist_traverse(s1, my_print);
    slist_destroy(s1);
#endif

#if 0
    printf("=============test slist_reverse===================\n");
    slist_reverse(s);
    slist_traverse(s, my_print);
    //slist_traverse(s, my_print);
    //printf("\n");
#endif

    //销毁链表
    slist_destroy(s);

    return 0;
err0:
    return -1;
}




